package com.cxj.baby;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.waps.AppConnect;
import com.waps.UpdatePointsNotifier;

public class MainActivity extends Activity implements UpdatePointsNotifier {
	private String[][] babies;
	private Spinner ageSpinner;
	private Spinner monthSpinner;
	private Button queryButton;
	private static final String TAG = "TAG";
	public static int currentPointTotal = 0;// 当前积分
	public static final int requirePoint = 60;// 要求积分
	public static boolean hasEnoughRequrePoint = false;// 是否达到积分

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 连接服务器. 应用启动时调用(为了统计准确性，此句必须填写).
		AppConnect.getInstance(this);

		List<String[]> tmpBabies = new ArrayList<String[]>(30);
		try {
			InputStream is = getResources().openRawResource(R.raw.baby);
			LineNumberReader fr = new LineNumberReader(new InputStreamReader(is));
			for (String line = fr.readLine(); line != null; line = fr.readLine()) {
				tmpBabies.add(line.split(","));
			}
			babies = tmpBabies.toArray(new String[0][]);
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}

		ageSpinner = (Spinner) findViewById(R.id.ageSpinner);

		ArrayAdapter<CharSequence> ageAdapter = ArrayAdapter.createFromResource(this, R.array.ages,
				android.R.layout.simple_spinner_item);
		ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ageSpinner.setAdapter(ageAdapter);

		monthSpinner = (Spinner) findViewById(R.id.monthSpinner);
		ArrayAdapter<CharSequence> monthAdapter = ArrayAdapter.createFromResource(this, R.array.months,
				android.R.layout.simple_spinner_item);
		monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		monthSpinner.setAdapter(monthAdapter);

		queryButton = (Button) findViewById(R.id.queryButton);
		queryButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				String ageStr = ageSpinner.getSelectedItem().toString();
				int age = Integer.valueOf(ageStr.substring(0, ageStr.length() - 1));
				String monthStr = monthSpinner.getSelectedItem().toString();
				int month = Integer.valueOf(monthStr.substring(0, monthStr.length() - 1));
				String sex = babies[age - 18][month - 1];
				int sexpng = sex.equals("男") ? R.drawable.boy : R.drawable.girl;
				int title = sex.equals("男") ? R.string.boy : R.string.girl;

				new AlertDialog.Builder(MainActivity.this).setIcon(sexpng).setTitle(title)
						.setPositiveButton("确认", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialoginterface, int i) {
							}
						}).show();
			}
		});

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.currentThread().sleep(1850 * 1000);
						AppConnect.getInstance(MainActivity.this).getPushAd();
					} catch (Exception e) {
						Log.e(TAG, e.getMessage());
					}
				}
			}
		}).start();
	}

	@Override
	protected void onDestroy() {
		AppConnect.getInstance(this).finalize();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		AppConnect.getInstance(this).getPoints(this);
		super.onResume();
	}

	/**
	 * AppConnect.getPoints()方法的实现，必须实现
	 * 
	 * @param currencyName
	 *            虚拟货币名称.
	 * @param pointTotal
	 *            虚拟货币余额.
	 */
	@Override
	public void getUpdatePoints(String currencyName, int pointTotal) {
		currentPointTotal = pointTotal;
		if (currentPointTotal >= requirePoint) {
			hasEnoughRequrePoint = true;
		}
	}

	/**
	 * AppConnect.getPoints() 方法的实现，必须实现
	 * 
	 * @param error
	 *            请求失败的错误信息
	 */
	@Override
	public void getUpdatePointsFailed(String error) {
		currentPointTotal = 0;
	}
}